<?
// gets info on a username(s) .. via ajax

require_once('../../../config.php');
global $CFG, $db;
// Fix for IE which craps out on charset=utf8
header("Content-Type: text/plain; charset=UTF-8");
if (!isloggedin())
	exit;

function var_dump_ret($mixed = null) {
  ob_start();
  var_dump($mixed);
  $content = ob_get_contents();
  ob_end_clean();
  return $content;
}

function ret($ok, $extra='')
{
        print "{\"ok\": $ok, \"extra\": \"" . $extra . "\" }";
}

function cook_to_json($fs, $fields)
{
	$resp = "";
	for ($i = 0; $i < count($fields); $i++)
	{
		$resp .= '"' . $fields[$i] . '": "' . $fs[$fields[$i]] . '"';
		if ($i < (count($fields)-1))
		{
			$resp .= ",";
		}
	}

	return "{" . $resp . "}";
}

$instr = "";

for ($i = 0; $i < count($_GET["usernames"]); $i++)
{
	$instr .= "'" . addslashes($_GET["usernames"][$i]) . "'";
	if ($i < (count($_GET["usernames"])-1))
		$instr .= ",";
}

//$db->debug = true;
$rs = $db->Execute("SELECT * FROM ".$CFG->prefix."user WHERE username IN ($instr)");

if (!$rs)
	ret(0);

if ($rs)
{
	$resp = "";
	while (!$rs->EOF)
	{
		$resp .= '"' . $rs->fields["username"] . '": ' . cook_to_json($rs->fields, array("email","skype", "firstname", "lastname"));
		$rs->MoveNext();

		if (!$rs->EOF)
			$resp .= ",";
	}

	ret(1, rawurlencode("{" . $resp . "}"));
}

?>
